package com.marquis.cloud.utils;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 简易HTTP请求
 *
 * @author tyq
 * @data 2016/1/11
 */
public final class SimpleHttpRequest {

    /**
     * 发送GET请求
     *
     * @param url 地址
     * @return
     */
    public static final String get(String url) {
        // 创建链接
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        try {
            // 执行请求
            CloseableHttpResponse response = httpClient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            // 响应结果
            String res = null;
            if (entity != null)
                res = EntityUtils.toString(entity);
            // 销毁
            EntityUtils.consume(entity);
            response.close();
            return res;
        } catch (ClientProtocolException cp) {
            return handleExceptionMessage(cp.getClass().getName(), "不支持的请求协议 - " + cp.getMessage());
        } catch (IOException e) {
            return handleExceptionMessage(e.getClass().getName(), e.getMessage());
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 发送POST请求
     *
     * @param url    地址
     * @param params 参数
     * @return
     */
    public static final String post(String url, Map<String, Object> params) {
        // 创建客户端
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 创建POST请求
        HttpPost httpPost = new HttpPost(url);
        List<NameValuePair> pairs = new ArrayList<NameValuePair>();
        if (params != null && !params.isEmpty()) {
            for (String key : params.keySet()) {
                Object value = params.get(key);
                if (value == null) continue;
                pairs.add(new BasicNameValuePair(key, String.valueOf(value)));
            }
        }
        try {
            // HTTP 表单实体
            HttpEntity json = new UrlEncodedFormEntity(pairs, "utf-8");
            httpPost.setEntity(json);

            // 执行请求
            CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
            // 响应实体
            HttpEntity responseEntity = httpResponse.getEntity();

            String res = null;
            if (responseEntity != null)
                res = EntityUtils.toString(responseEntity, "utf-8");
            // 销毁
            EntityUtils.consume(responseEntity);
            httpResponse.close();
            // 返回
            return res;

        } catch (UnsupportedEncodingException ue) {
            return handleExceptionMessage(ue.getClass().getName(), "不支持的字符编码 - " + ue.getMessage());
        } catch (ClientProtocolException cp) {
            return handleExceptionMessage(cp.getClass().getName(), "不支持的请求协议 - " + cp.getMessage());
        } catch (IOException e) {
            return handleExceptionMessage(e.getClass().getName(), e.getMessage());
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 处理异常信息
     *
     * @param exception
     * @param msg
     * @return
     */
    private static final String handleExceptionMessage(String exception, String msg) {
        return "{\"exception\":\"" + exception + "\", \"message\": \"" + msg + "\"}";
    }
}
